<?php
/**
 * @authors alex (20779512@qq.com)
 * @date    2014-03-24 17:25:39
 * @version 1.0
 */

class BaoxianAction extends HomeAction {

    public function index(){
    	$map['memberID'] = $this->_user['memberID'];
        $map['payStatus'] = 1;
        $map['del'] = 0;
        $map['baoxian'] = array('gt',0);
        $order_no = M('Order')->where($map)->getField('id,order_no');

        unset($map);
        $map['order_no'] = array('in',$order_no); 
        $map['hasBaoxian'] = 1; 
        $list= M('OrderDetail')->where($map)->order('baoxian asc')->select();
        
        foreach ($list as $key => $value) {
            $list[$key]['child'] = M('Baoxian')->field('chechuanshui,money,payStatus')->where('order_no="'.$value['order_no'].'"')->find();
        }
        $this->assign('list', $list);
        $this->display();
    }

    public function add(){
        $id = I('get.id');
        if ($id=='') {
            $this->error('参数错误');
        }
        $map['id'] = $id;
        $map['baoxian'] = 0;
        $list = M('OrderDetail')->where($map)->find();
        if (!$list) {
            $this->error('没有该订单');
        }
        unset($map);
        $map['order_no'] = $list['order_no'];
        $order = M('Order')->where($map)->find();
        if ($order) {
            if ($order['payStatus']==0) {
                $this->error('订单尚未支付，不能申请');
            }else{
                unset($map);
                $map['memberID'] = $this->_user['memberID'];
                $address = M('Address')->where($map)->order('def desc , id desc')->find();
                $this->assign('address',$address);
                $this->assign('list',$list);
                $this->display();
            }            
        }else{
            $this->error('没有该订单');
        }        
    }

    public function save(){
        $obj = D('Baoxian');
        if ($data = $obj->create()) {

            if ($data['shangpai']==0 && $data['card5']=='') {
                echo $this->echo_json_str('请上传车辆合格证',$message,$url); 
                die;
            }

            if ($data['chechuanshui']==1 && $data['card6']=='') {
                echo $this->echo_json_str('请上传完税证明',$message,$url); 
                die;
            }

            //4位随机数
            $curDateTime = date("YmdHis");
            $randNum = rand(1000, 9999);
             /* 商家的定单号 */
            $data['bx_no'] = $curDateTime . $randNum;

            if ($list = $obj->add($data)) {
                M('OrderDetail')->where('id='.I('post.detailID'))->setField('baoxian','1');
                $state = 'SUCCESS';
                
                if ($data['chechuanshui']==0) {
                    $message = '提交成功，前去支付';
                    $url = U('Baoxian/pay',array('bx_no'=>$data['bx_no']));
                }else{
                    $message = '操作成功';
                    $url = U('Baoxian/index');
                }
                                
            } else {
                $state = '操作失败';
            }
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url); 
    }

    public function view(){
        $id = I('get.id');
        $map['detailID'] = $id;
        $map['memberID'] = $this->_user['memberID'];
        $list = M('Baoxian')->where($map)->find();
        if ($list) {
            $where['id'] = $id;
            $list['shop']=M('OrderDetail')->where($where)->find();
            $this->assign('list',$list);
            $this->display();
        }else{
            $this->error("没有该信息");
        }
    }

    public function pay(){
        $bx_no = I('bx_no');
        if ($bx_no=='') {
            $this->error('订单不能为空');
        }else{
            $map['bx_no'] = $bx_no;
            $list = M('Baoxian')->where($map)->find();
            if (!$list) {
                $this->error('订单不存在');
            }else{
                if ($list['payStatus']==1) {
                    $this->eror('订单已支付');
                }else{
                    $this->assign('list',$list);
                    $this->display();
                }
            }
        }        
    }
}